RUST Documentation 따라 공부하기 4
2019.12.30
Functions
Rust code uses snake case as the conventional style for function and variable names. In snake case, all letters are lowercase and underscores separate words.
If we define
another_functionafter themainfunction, it doesn't matter. We could have defined it before as well. Rust doesn’t care where you define your functions, only that they’re defined somewhere.
Pass parameters
fn main() {
another_function(5, 6);
}
fn another_function(x: i32, y: i32) {
println!("The value of x is: {}", x);
println!("The value of y is: {}", y);
}
Function Bodies Contain Statements and Expressions
Statements are instructions that perform some action and do not return a value. Expressions evaluate to a resulting value.
fn main() {
let y = 6;
}
function definitions are statements.
let y = 6; is statement.
Statement do not return values => can't assign let statement to another value
ex) let x = (let y = 6); // x
In Rust, such an expression "x = y = 6;" can't happen.
Expressions can be part of statements.
fn main() { let x = 5; let y = { let x = 3; x + 1 // **do not include semicolon** } println!("y : {}", y); // y : 4 }Expressions do not include ending semicolons.
-> If I put semicolon, then it doesn't return. It becomes statement.
fn main() { let x = plus_one(5); println!("The value of x is: {}", x); } fn plus_one(x: i32) -> i32 { x + 1 // ** don't put semicolon // semicolon을 붙이면 나는 에러 : type 이 맞지 않는다는 것 : found type () }